/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package utilities;

import utilities.entities.SCalendar;
import bdl.MatchBean;
import entities.Match;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
import matpred.CalendarPanel;
import utilities.entities.Result;
import utilities.entities.Result.RowMatch;

/**
 *
 * @author olawale
 */
public class Utility {

    private static String [] months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
                                        "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

    private static SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy");
    private static SCalendar defCal = CalendarPanel.getCalendarPanel().getDefaultCalendar();

    public static int findRow(DefaultTableModel dtm, String date){
        String fdate = (String) dtm.getValueAt(1, 1);
        if(fdate.substring(0, 3).equals(date.substring(0, 3))){
            for(int r = 0; r < dtm.getRowCount(); r++){
                String cdate = (String) dtm.getValueAt(r, 1);
                if(cdate.equals(date)){
                    return r;
                }
            }
        }
        return -9;
    }


    public static List<RowMatch> compare(SCalendar scal,String sdate, String edate, int crow){
        DefaultTableModel dtm = scal.getTable();
        int startRow = Utility.findRow(dtm, sdate);
        int endRow = Utility.findRow(dtm, edate);
//        System.out.println("calendar = " + scal.getName());
//        System.out.println("start date = " + sdate + " endRow = " + edate);
//        System.out.println("start row = " + startRow + " endRow = " + endRow);
        
        ArrayList<RowMatch> resList = new ArrayList();
        for(int row = startRow; row <= endRow; row++){
            int nmatch = 0;
            Result.RowMatch rowMatch = new Result().new RowMatch();
            HashMap<Integer, Integer> colVal = new HashMap();
            for(int col = 0; col < dtm.getColumnCount(); col++){
                if(col < 5) continue;
                Object [] val = (Object[]) dtm.getValueAt(row, col);
                Object [] cval = (Object[]) dtm.getValueAt(crow, col);
                if(val[0] == cval[0]){
                    ++nmatch;
                    int colNo = (2 * (col - 4)) - 1;
                    int colEntry = Integer.parseInt(val[0].toString());
                    colVal.put(colNo, colEntry);
                }
                if(val[1] == cval[1]){
                    ++nmatch;
                    int colNo = (2 * (col - 4));
                    int colEntry = Integer.parseInt(val[1].toString());
                    colVal.put(colNo, colEntry);
                }

            }

            if(nmatch > 0){
                String date = (String) dtm.getValueAt(row, 1);
//                System.out.println(parseDate(date) + " row number = " + row + " nmatch = " + nmatch);
                rowMatch.colVal = colVal;
                rowMatch.row = row;
                rowMatch.sdate = date;
                resList.add(rowMatch);
//                System.out.println("column entries = " + colVal);
            }
        }
//        System.out.println("total column matches= " + resList.size());

        return resList;
    }


    public static Date parseDate(String dateString){
        //Mon, 31 Jan 2000
        if(dateString.length() < 15) return null;
        ArrayList<String> mn = new ArrayList();
        for(String m : months){
            mn.add(m);
        }
        Calendar cal = Calendar.getInstance();
        int d = 0, m = 0, y = 0;
        try{
            d = Integer.parseInt(dateString.substring(5, 7));
            m = mn.indexOf(dateString.substring(8, 11));
            y = Integer.parseInt(dateString.substring(12));
        }catch(Exception ex){
            System.out.println(ex.toString());
        }
        cal.set(y, m, d);
        Date date = cal.getTime();
        return date;
    }

    public static String parseDate(Date date){
        return sdf.format(date);
    }

    public static Object [] f_subract(Match m, Result res){
        Vector list = new Vector();
        if(m == null || res == null){
            return null;
        }
        
        String tmp = "";
        String fixture = m.getFixture().toString();
        fixture = (fixture.length() == 1) ? ("0"+fixture) : fixture;
        tmp = tmp + fixture;

        int ser = Integer.parseInt(res.aResult.calander.getSerial(m.getMatchDate()));
        ser = ser - m.getFixture();
        String d = res.aResult.calander.getDate(ser);
        String a, h;
        try{
            h = MatchBean.getMatches(parseDate(d), m.getFixture()).get(0).getHomeScore().toString();
            a = MatchBean.getMatches(parseDate(d), m.getFixture()).get(0).getAwayScore().toString();
        }catch(Exception ex){
            a = "-";
            h = "-";
            return list.toArray();
        }

        tmp = tmp + a + h;

        Calendar cal = Calendar.getInstance();
        cal.setTime(parseDate(d));
        String da = cal.get(Calendar.DAY_OF_MONTH) + "";
        da = (da.length() == 1) ? ("0"+da) : da;
        String mo = (cal.get(Calendar.MONTH) + 1) + "";
        mo = (mo.length() == 1) ? ("0"+mo) : mo;


        tmp = tmp + da;
        tmp = tmp + mo;
        tmp = tmp + cal.get(Calendar.YEAR);
        tmp = tmp + ser;

        for(char c : tmp.toCharArray()){
            list.add(c);
        }
        return list.toArray();
    }

    public static Object [] f_added(Match m, Result res){
        Vector list = new Vector();
        if(m == null){
            return null;
        }

        String tmp = "";
        String fixture = m.getFixture().toString();
        fixture = (fixture.length() == 1) ? ("0"+fixture) : fixture;
        tmp = tmp + fixture;
        
        int ser = Integer.parseInt(res.aResult.calander.getSerial(m.getMatchDate()));
        ser = ser + m.getFixture();
        String d = res.aResult.calander.getDate(ser);
        String a, h;
        try{
            h = MatchBean.getMatches(parseDate(d), m.getFixture()).get(0).getHomeScore().toString();
            a = MatchBean.getMatches(parseDate(d), m.getFixture()).get(0).getAwayScore().toString();
        }catch(Exception ex){
            a = "-";
            h = "-";
            return list.toArray();
        }

        tmp = tmp + a + h;

        Calendar cal = Calendar.getInstance();
        cal.setTime(parseDate(d));
        String da = cal.get(Calendar.DAY_OF_MONTH) + "";
        da = (da.length() == 1) ? ("0"+da) : da;
        String mo = (cal.get(Calendar.MONTH) + 1) + "";
        mo = (mo.length() == 1) ? ("0"+mo) : mo;


        tmp = tmp + da;
        tmp = tmp + mo;
        tmp = tmp + cal.get(Calendar.YEAR);
        tmp = tmp + ser;

        for(char c : tmp.toCharArray()){
            list.add(c);
        }
        return list.toArray();
    }

    public static Object [] b_added(Match m){
        Vector list = new Vector();
        Random r = new Random();
        if(m == null){
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(m.getMatchDate());
        String tmp = "";
        tmp = (m.getFixture().toString().length() == 1) ? (" " + m.getFixture().toString())
                : m.getFixture().toString();
        tmp = tmp + cal.get(Calendar.DAY_OF_MONTH);
        tmp = tmp + (cal.get(Calendar.MONTH) + 1);
        tmp = tmp + cal.get(Calendar.YEAR);
        tmp = tmp + m.getFixture();
        tmp = tmp + defCal.getSerial(m.getMatchDate()) + r.nextInt(19);

        for(char c : tmp.toCharArray()){
            list.add(c);
        }
        return list.toArray();
    }

    public static int[] getNumbers(String list){
        if(list.contains("-")){
            String [] numb = list.split("-");
            int start = Integer.parseInt(numb[0].trim());
            int end = Integer.parseInt(numb[1].trim());
            int length = end - start + 1;
            int [] numbers = new int[length];
            for(int i=0; i<numbers.length; i++){
                numbers[i] = start++;
            }
            return numbers;
        }else{
            int number = Integer.parseInt(list.trim());
            return new int[]{number};
        }
    }

    public static SCalendar impose(SCalendar sc1, SCalendar sc2){
        DefaultTableModel table1 = sc1.getTable();
        DefaultTableModel table2 = sc2.getTable();
        String name = defCal.getName() + " " + sc1.getNumber() + "/" + sc2.getNumber();
        SCalendar ical = new SCalendar(table1, name, false, -1);
        DefaultTableModel itable = ical.getTable();
        int count = (sc1.getRowCount() < sc2.getRowCount()) ? sc1.getRowCount() : sc2.getRowCount();
        for(int row = 0; row < count; row++){
            for(int col = 5; col < itable.getColumnCount(); col++){
                Object [] val = (Object[]) table2.getValueAt(row, col);
                if(val[0].toString().equals("") || val[0].toString().equals("")){
                    continue;
                }
                itable.setValueAt(val, row, col);
            }
        }
        return ical;
    }

    public static int parseInt(String str){
        try{
            int res = Integer.parseInt(str);
            return res;
        }catch(Exception ex){}
        return -1;
    }

    public static DefaultTableModel incrMonitors(DefaultTableModel dtm, int incr){
        if(incr == 0) return dtm;
        for(int row = 0; row < dtm.getRowCount(); row++){
            try{
                int a = Integer.parseInt(dtm.getValueAt(row, 2).toString()); a+=incr;
                int b = Integer.parseInt(dtm.getValueAt(row, 3).toString()); b+=incr;
                dtm.setValueAt(a, row, 2);
                dtm.setValueAt(b, row, 3);
            }catch(Exception ex){
                continue;
            }
        }
        return dtm;
    }

}
